//
//  NextViewController.m
//  block不需要使用weakSelf
//
//  Created by chenleping on 2018/12/17.
//  Copyright © 2018年 DSY. All rights reserved.
//

#import "NextViewController.h"

#import <Masonry.h>
#import <AFHTTPSessionManager.h>

#import "CustomBtn.h"

@interface NextViewController ()

@property (nonatomic, copy) NSString * name;

@property (nonatomic, strong) UIView * redView;
@property (nonatomic, strong) UIView * blackView;
@property (nonatomic, strong) CustomBtn * customBtn;

@property (nonatomic, strong)  AFHTTPSessionManager * manager;

@end

@implementation NextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor yellowColor];
    
    self.name = @"我的名字叫什么？";
    
    // GCD
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"===%@===", self.name);
        [self doSomeThing];
    });
    
    // Masonry
    [self.view addSubview:self.blackView];
    [self.blackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self.view);
        make.width.equalTo(@(self.view.frame.size.width / 3));
    }];
    
    UIView * redView = [[UIView alloc] init];
    [self.view addSubview:redView];
    redView.backgroundColor = [UIColor redColor];
    
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.blackView.mas_right);
        make.top.bottom.equalTo(self.view);
        make.width.equalTo(self.blackView.mas_width);
    }];
    
    // AFNetworking
    [self.manager GET:@"http://cas.cs.teacheredu.cn/edu/proj/tlogin/mobile/v370/MobileStatistics/GetHotCity.json?" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"===请求数据成功===");
        [self doSomeThing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    [self.view addSubview:self.customBtn];
    self.customBtn.frame = CGRectMake(0, 0, 100, 100);
    
}

- (void)doSomeThing{
    NSLog(@"在dispatch里做些什么...");
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)dealloc{
    NSLog(@"我被释放了...");
}

- (UIView *)blackView{
    if (_blackView == nil) {
        _blackView = [UIView new];
        _blackView.backgroundColor = [UIColor blackColor];
    }
    return _blackView;
}
- (AFHTTPSessionManager *)manager{
    if (_manager == nil) {
        _manager = [AFHTTPSessionManager manager];
    }
    return _manager;
}
- (CustomBtn *)customBtn{
    if (_customBtn ==  nil) {
        _customBtn = [CustomBtn buttonWithType:UIButtonTypeCustom];
        _customBtn.backgroundColor = [UIColor whiteColor];
        
        // 循环引用
        _customBtn.clickBlock = ^{
            NSLog(@"%@clickBlock回调...",self);
        };
        
        [_customBtn clickBtn:^{
            NSLog(@"%@clickBlock回调...",self);
            [self doSomeThing];
        }];
    }
    return _customBtn;
}

@end
